//app.js
var api = require("./utils/api");
import request from './utils/request'
import {
  getToken
} from "./utils/util.js"
App({
  onLaunch: function () {
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  getRoute() {
    let self = this
    let res = this.globalData.loginInfo
    var url = api.SecretLogin;
    var token = getToken()
    var params = {
      access_token: token
    };
    request(url, params, "POST").then(result => {
      if (result.code === 0) {
        self.globalData.loginInfo = result.data;
        self.checkRoute(result.data)
      }
    });
  },
  //检测路由与当前路由是否一致
  mutRoute(url) {
    var p = getCurrentPages()
    var route = p.pop().__route__
    if (url === "/" + route) {
      return true
    } else {
      return false
    }
  },
  checkRoute(res) {
    var url = ''
    switch (res.isadmin) {
      case 0:
        url = '/pages/index/index'
        break;
      case 1:
        url = '/pages/checkIndexk/checkIndexk'
        break;
      case 2:
        url = '/pages/checkIndex/checkIndex'
        break;
      case 3:
        url = '/pages/checkIndex/checkIndex'
        this.globalData.isBoss = true
        break;
      default:
        url = '/pages/loginWeb/loginWeb'
    }
    if (!this.mutRoute(url)) {
      wx.reLaunch({
        url: url
      })
    }
  },
  checkOpenid() {
    this.getLocalData()
    if (this.globalData.loginInfo && this.globalData.loginInfo.openid) {
      return true
    } else {
      return false
    }
  },
  userLogin(enData) {
    var self = this;
    let promise = new Promise(function (resolve, reject) {
      wx.login({
        success: function (res) {
          if (res.code) {
            var token = getToken()
            var params = {
              access_token: token,
              code: res.code,
            };
            var url = api.SecretLogin;
            request(url, params, "POST").then(result => {
              if (result.code === 0) {
                var saveData = {
                  openid: result.data.openid,
                  isShow: result.data.isShow
                };
                self.globalData.loginInfo = saveData;
                try {
                  wx.setStorageSync('loginInfo', saveData)
                } catch (e) {}
                resolve(result.data); // 返回登录状态
              }
            });
          } else {
            resolve(false); // 返回登录状态 登录失败
          }
        }
      });
    });
    return promise;
  },
  // 获取本地保存的数据
  getLocalData() {
    var self = this
    try {
      self.globalData.loginInfo = wx.getStorageSync('loginInfo')
    } catch (e) {
      console.log(e)
    }
  },
  globalData: {
    backPage: false, // 是否从上个页面返回
    loginInfo: {},
    fileArr: [], // 上传文件返回的文件路径列表
    sendWebFileArr: '',
    isBoss: false,
    nexPageData:{}, //下页面数据
  }
})